﻿using System;
using System.IO;
using Windows.Storage;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;

namespace XnReader.WinRT.Helper
{
    public class DateToStringConverter : IValueConverter
    {

        #region IValueConverter Members

        // Define the Convert method to change a DateTime object to 
        // a month string.
        public object Convert(object value, Type targetType,
            object parameter, string language)
        {
            var thisdate = (DateTime) value;

            return thisdate.ToString("MM/dd HH:mm");
        }

        // ConvertBack is not implemented for a OneWay binding.
        public object ConvertBack(object value, Type targetType,
            object parameter, string language)
        {
            throw new NotImplementedException();
        }


        #endregion
    }

    /// <summary>
    /// 背景色转换器
    /// </summary>
    public class BackgroundRandomConverter : IValueConverter
    {
        private readonly SolidColorBrush[] _colorBrushes = new SolidColorBrush[]
                                                               {
                                                                   new SolidColorBrush(Color.FromArgb(100, 17, 174, 194))
                                                                   ,
                                                                   new SolidColorBrush(Color.FromArgb(100, 138, 174, 16))
                                                                   ,
                                                                   new SolidColorBrush(Color.FromArgb(100, 83, 252, 194))
                                                                   ,
                                                                   new SolidColorBrush(Color.FromArgb(100, 233, 171, 22))
                                                                   ,
                                                                   new SolidColorBrush(Color.FromArgb(100, 226, 95, 35))
                                                                   ,
                                                                   new SolidColorBrush(Color.FromArgb(100, 36, 188, 206))
                                                                   ,
                                                                   new SolidColorBrush(Color.FromArgb(100, 181, 212, 171))
                                                                   ,
                                                                   new SolidColorBrush(Color.FromArgb(100, 2, 156, 50))
                                                                   //默认
                                                               };

        public object Convert(object value, Type targetType, object parameter, string language)
        {
            var id = (int) value;
            if (id < _colorBrushes.Length)
                return _colorBrushes[id];
            else
                return _colorBrushes[_colorBrushes.Length - 1];

        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 书架指示当前标识是否显示
    /// </summary>
    public class BookcaseFlagVisibleConverter:IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            var display = (int)value;

            return display.Equals(1) ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 封面转换器
    /// </summary>
    public class CoverConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            var articleid = value.ToString();
            if (!string.IsNullOrEmpty(articleid))
            {
                var tmpPath = string.Format(@"{0}\cover\{1}.jpg", Path.Combine(ApplicationData.Current.LocalFolder.Path),
                                            articleid);
                if (Local.Service.Utils.FileExists(tmpPath))
                {
                    var img = new BitmapImage(new Uri(tmpPath));
                    return img;
                }
            }
            return new BitmapImage(new Uri("ms-appx:///Assets/defaultcover.jpg"));
        }


        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// Online封面转换器
    /// </summary>
    public class OnlineCoverConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            var uri = value.ToString();
            if (!string.IsNullOrEmpty(uri))
            {
                var img = new BitmapImage(new Uri(uri));
                return img;
            }
            return new BitmapImage(new Uri("ms-appx:///Assets/defaultcover.jpg"));
        }


        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }


    /// <summary>
    /// 书架、阅读历史图片
    /// </summary>
    public class BookcaseImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            var id = value.ToString();
            if (id.Equals("0"))
            {
                return "";
            }
            else
            {
                return "";
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
    
    /// <summary>
    /// 书架、阅读历史图片
    /// </summary>
    public class BookcaseBackColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            var id = value.ToString();
            if (id.Equals("0"))
            {
                return new SolidColorBrush(Colors.RosyBrown);
            }
            else
            {
                return new SolidColorBrush(Colors.Green);
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// Online类别Icon转换器
    /// </summary>
    public class OnlineSortIconConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            var sortid = value.ToString();
            if (!string.IsNullOrEmpty(sortid))
            {
                switch (sortid)
                {
                    case "1001":
                        return "⛺";
                    case "1":
                        return "✡";
                    case "2":
                        return "☯";
                    case "3":
                        return "⚭";
                    case "4":
                        return "☭";
                    case "5":
                        return "⚽";
                    case "6":
                        return "⚛";
                    case "7":
                        return "⚘";
                    case "101":
                        return "❁";
                    case "1002":
                        return "☉";
                    case "1003":
                        return "☺";
                }
            }
            return "⛺";
        }


        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 工具栏style转换器
    /// </summary>
    public class ToolBarStyleConverter:IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            var theme = value.ToString();
            return theme.Equals("white") ? Application.Current.Resources["StyleToolBar"] : Application.Current.Resources["AppBarButtonStyle"];
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 阅读背景转换器
    /// </summary>
    public class ReadBackgroundConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            string imgPath = string.Format("ms-appx:///Assets/theme/view{0}.jpg", value.ToString());
            return new BitmapImage(new Uri(imgPath));
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 阅读前景色转换器
    /// </summary>
    public class FontColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            return value.ToString().Equals("white")
                       ? new SolidColorBrush(Colors.White)
                       : new SolidColorBrush(Colors.Black);
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 阅读页字体转换器
    /// </summary>
    public class FontFamilyConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            return new FontFamily(value.ToString());
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
}
